import Taro from '@tarojs/taro';
import u from 'updeep'
import request from '../util/request'

export default {
  namespace: 'recruitmentList',
  state: {
    listData: [], // 列表数据
    districtType: {
      id: 0,
      name: '区域'
    },
    districtTypeRange: [],
    limit: 8,
    offset: 0,
    total: 0, // 总数
    type: 0, //0:求职，1：招聘
    typeList: [
      {
        value: 0,
        label: '求职'
      }, {
        value: 1,
        label: '招聘'
      }
    ]
  },
  reducers: {
    updateState(state, {payload}) {
      return u({
        ...payload
      }, state)
    },
    initData(state, action) {
      return {
        ...state,
        ...action.payload,
        listData: [],
        offset: 0,
        total: 0, // 总数
      }
    }
  },
  effects: {
    // 获取列表数据
    * loadList({payload = {}}, {select, put, call}) {
      const {
        limit, listData, type, districtType
      } = yield select(({recruitmentList}) => recruitmentList)
      const param = {
        limit, type,
        offset: listData.length,
        districtId: districtType.id
      }
      let url = 'service/recruit/getPage';

      const {data} = yield call(request, {
        url: SERVERURL + url,
        data: param,
        method: 'post',
      })

      if (data && data.rows instanceof Array) {

        if (data.rows.length == 0) {
          Taro.showToast({
            title: (data || {}).msg || '没有更多数据',
            icon: 'none'
          })
        } else {
          const d = data.rows || {};
          yield put({
            type: 'updateState',
            payload: {
              listData: listData.concat(d),
              total: data.total
            }
          })
        }

      } else {
        Taro.showToast({
          title: (data || {}).msg || '获取列表数据失败',
          icon: 'none'
        })
      }
    },
    * loadDistrict({payload = {}}, {put, call}) {
      const {data} = yield call(request, {
        url: SERVERURL + '/service/city/all',
        method: 'get',
      })
      if (data && data.data instanceof Array) {
        const d = data.data || {};
        yield put({
          type: 'updateState',
          payload: {
            districtTypeRange: d
          }
        })
      } else {
        Taro.showToast({
          title: (data || {}).msg || '获取列表数据失败',
          icon: 'none'
        })
      }
    }
  }
};
